package iothub

import (
	"gitee.com/arjunxw/iothub-south-client/config"
	"gitee.com/arjunxw/iothub-south-client/message"
	"gitee.com/arjunxw/iothub-south-client/request"
	"gitee.com/arjunxw/iothub-south-client/service"
	"github.com/google/wire"
	"go.uber.org/zap"
)

var ProviderSet = wire.NewSet(NewApp)

var AllProviderSet = wire.NewSet(NewApp, message.NewIotHubMessage, request.NewRequest, service.NewDalInfoService, service.NewReportService)

type App struct {
	cfg            config.Config
	log            *zap.Logger
	dalInfoService service.DalInfoService
	reportService  service.ReportService
	message        message.IotHubMessage
	request        request.Request
}

func NewApp(cfg config.Config, log *zap.Logger,
	message message.IotHubMessage, request request.Request,
	dalInfoService service.DalInfoService, reportService service.ReportService) *App {
	return &App{cfg: cfg, log: log, message: message,
		request: request, dalInfoService: dalInfoService, reportService: reportService}
}

func (app *App) Run() (err error) {
	if c, ok := app.dalInfoService.(interface{ Init() error }); ok {
		if err = c.Init(); err != nil {
			return
		}
	}
	if c, ok := app.reportService.(interface{ Init() error }); ok {
		if err = c.Init(); err != nil {
			return
		}
	}
	if c, ok := app.message.(interface{ Init() error }); ok {
		if err = c.Init(); err != nil {
			return
		}
	}
	if c, ok := app.request.(interface{ Init() error }); ok {
		if err = c.Init(); err != nil {
			return
		}
	}
	return
}

func (app *App) GetConfig() config.Config {
	return app.cfg
}

func (app *App) GetLogger() *zap.Logger {
	return app.log
}

func (app *App) GetIothubMessage() message.IotHubMessage {
	return app.message
}

func (app *App) GetRequest() request.Request {
	return app.request
}

func (app *App) GetDalInfoService() service.DalInfoService {
	return app.dalInfoService
}

func (app *App) GetReportService() service.ReportService {
	return app.reportService
}
